const config = require('../../../../utils/config.js');
var QQMapWX = require('../../../../utils/qqmap.js');
var qqMap = new QQMapWX({
  key: config.key
});
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabList: config.tabList,
    currentTitle:'地铁',
    currentTabState:0,
    latitude: "",
    longitude: "",
    currentMarkers:{},
    markers: []
  },
  checkTab(e){
    let that = this;
    let index = e.currentTarget.dataset.index;
    let title = e.currentTarget.dataset.title;
    that.setData({
      currentTabState: index,
      currentTitle:title
    })

    that.mapSearch();

  },
  mapSearch(){
    let that = this;
    qqMap.search({
      keyword: that.data.currentTitle,  //搜索关键词
      location: `${that.data.latitude},${that.data.longitude}`,  //设置周边搜索中心点
      page_size:20,
      success: function (res) { //搜索成功后的回调
        var mks = []
        for (var i = 0; i < res.data.length; i++) {
          mks.push({ // 获取返回结果，放到mks数组中
            title: res.data[i].title,
            id: res.data[i].id,
            latitude: res.data[i].location.lat,
            longitude: res.data[i].location.lng,
            iconPath: that.data.tabList[that.data.currentTabState].markerIcon, //图标路径
            width: 30,
            height: 36,
            callout: {
              content: res.data[i].title,
              display: 'ALWAYS',
              fontSize: 12,
              color: '#222',
              padding: 5
            }
          })
        }
        mks.unshift(that.data.currentMarkers)

        that.setData({ //设置markers属性，将搜索结果显示在地图中
          markers: mks
        })
      },
      fail: function (res) {
        console.log(res);
      },
      complete: function (res) {
        console.log(res);
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let latitude = options.latitude;
    let longitude = options.longitude;
    let currentTitle = options.currentTitle;
    let currentTabState = options.currentTabState;

    this.setData({
      latitude: latitude,
      longitude: longitude,
      currentTitle: currentTitle,
      currentTabState: currentTabState,
      currentMarkers: {
        id: 0,
        latitude: latitude,
        longitude: longitude,
        iconPath: "/images/marker.png",
        width: 20,
        height: 20
      },
    })
    this.mapSearch();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
  },
  
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    app.globalData.mapTabStatusIndex = this.data.currentTabState
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },
})